home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
oath.lha
/
oath
/
src
/
hashSet.cc
< prev
next >
Wrap
C/C++ Source or Header
|
1991-08-29
|
6KB
|
241 lines
//***************************************************************************
// OATH :: Object-oriented Abstract Type Hierarchy
//***************************************************************************
//
// Copyright (C) 1991 Texas Instruments Incorporated
// Permission is granted to any individual or institution
// to use, copy, modify, and distribute this software,
// provided that this complete copyright and permission notice
// is maintained, intact, in all copies and supporting documentation.
//
// Texas Instruments Incorporated provides this software "as is"
// without express or implied warranty.
//
//***************************************************************************
// hashSet (hashSetA, hashSetG)
//
// History:
// 07/91 Brian M Kennedy import, export, typeRegister
// 06/91 Brian M Kennedy New macros & format; remove printDiagnostic
// 04/91 Brian M Kennedy Original
//
//***************************************************************************
#include "copyright.h"
#include <oath/hashSet.h>
#include <iostream.h>
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// hashSet Outlines
OUTLINES(hashSet, set)
// Constructors //////////
hashSetG::
hashSetG (unsigned int IBuckets, int IsConst)
:setG(IsConst), Set(0), Count(0), Buckets(IBuckets)
{ref();
Set = (hashNodeP**)(new hashNodeP* [IBuckets]);
for(unsigned int I = 0; I < Buckets; I++)
Set[I] = 0;
deref();
}
// oathCore Operations //////////
void hashSetG::
export (exportP& X) const
{X.writeType(TypeName);
X.stream() << Buckets << ' ' << Count << (isConst() ? ' ' : '\0');
for(unsigned int I = 0; I < Buckets; ++I)
for(hashNodeP* N = Set[I]; N; N = N->Next)
N->Obj.export(X);
}
objA hashSetG::
import (importP& M)
{int Buckets, Count;
M.stream() >> Buckets >> Count;
char MakeConst = M.stream().get();
hashSetA HS = hashSetA::make(Buckets);
for(int I = 0; I < Count; ++I)
HS << objA::import(M);
if(MakeConst)
HS.guts()->forceConst();
return HS;
}
void hashSetG::
clearReferences()
{clearMark();
for(unsigned int I = 0; I < Buckets; ++I)
for(hashNodeP* N = Set[I]; N; N = N->Next)
N->Obj.guts()->deref();
}
void hashSetG::
setReferences()
{if(!isMarked())
{setMark();
for(unsigned int I = 0; I < Buckets; ++I)
{for(hashNodeP* N = Set[I]; N; N = N->Next)
{N->Obj.guts()->ref();
N->Obj.guts()->setReferences();
}
}
}
}
// obj Operations //////////
int hashSetG::
isEqual (const objG* O) const
{if(!O->isType(setG::Type))
return FALSE;
else
{const setG* S = (const setG*)O;
if(Count != S->count())
return FALSE;
else
{for(unsigned int I = 0; I < Buckets; ++I)
{for(hashNodeP* N = Set[I]; N; N = N->Next)
{if(!S->contains(N->Obj.guts()))
return FALSE;
}
}
return TRUE;
}
}
}
objA hashSetG::
makeCopy (int) const // ignores MakeConst argument
{bagA HS = makeEmpty();
applyX(callSelf, HS.guts());
return HS;
}
// bag Operations //////////
int hashSetG::
contains (const objG* O) const
{unsigned int Hash = hash(O);
return Set[Hash] && Set[Hash]->contains(O);
}
int hashSetG::
containsEqual (const objG* O) const
{ensure(contains(O), "Sorry, not implemented.");
return TRUE;
}
int hashSetG::
canContain (const objG*) const
{return TRUE;}
void hashSetG::
insert (const objG* O)
{NOT_CONST();
unsigned int Hash = hash(O);
Set[Hash] = new hashNodeP(O, Set[Hash]);
}
void hashSetG::
append (const bagG* B)
{NOT_CONST();
B->applyX(callSelf, this);
}
void hashSetG::
apply (void (*F)(objA)) const
{for(unsigned int I = 0; I < Buckets; ++I)
for(hashNodeP* N = Set[I]; N; N = N->Next)
F(N->Obj);
}
bagG* hashSetG::
applyX (objA (*F)(objA), bagG* B) const
{for(unsigned int I = 0; I < Buckets; ++I)
{for(hashNodeP* N = Set[I]; N; N = N->Next)
{objA R = F(N->Obj);
B->insert(R.guts());
}
}
return B;
}
bagG* hashSetG::
applyX (bagA (*F)(objA), bagG* B) const
{for(unsigned int I = 0; I < Buckets; ++I)
{for(hashNodeP* N = Set[I]; N; N = N->Next)
{bagA R = F(N->Obj);
B->append(R.guts());
}
}
return B;
}
bagA hashSetG::
makeEmpty () const
{return new hashSetG (Buckets);}
// Set Operations //////////
void hashSetG::
remove (const objG* O)
{NOT_CONST();
unsigned int Hash = hash(O);
if(Set[Hash])
Set[Hash]->remove(O, Set[Hash]);
}
void hashSetG::
subtract (const setG* S)
{NOT_CONST();
for(unsigned int I = 0; I < Buckets; ++I)
{hashNodeP** L = &(Set[I]);
hashNodeP* N = *L;
while(N)
{if(S->contains(N->Obj.guts()))
{*L = N->Next;
delete N;
N = *L;
}
else
{L = &(N->Next);
N = *L;
}
}
}
}
void hashSetG::
intersect (const setG* S)
{NOT_CONST();
for(unsigned int I = 0; I < Buckets; ++I)
{hashNodeP** L = &(Set[I]);
hashNodeP* N = *L;
while(N)
{if(!S->contains(N->Obj.guts()))
{*L = N->Next;
delete N;
N = *L;
}
else
{L = &(N->Next);
N = *L;
}
}
}
}
//***************************************************************************